<template>
  <div class="app-container flex-col minH">
    <div class="topline w100p bgwhite ptb20 radius10 pl10 pr20 justify-between">
      <div class="align-center flex-wrap">
        <el-input v-model="uid" style="width: 280px" placeholder="搜尋用戶UID..." :prefix-icon="Search" class="search mr10 mb10" />
        <el-input v-model="touid" style="width: 280px" placeholder="搜尋陪玩UID..." :prefix-icon="Search" class="search mr10 mb10" />
        <el-input v-model="order_no" style="width: 280px" placeholder="搜尋訂單號..." :prefix-icon="Search" class="search mr10 mb10" />

        <el-select v-model="state" placeholder="選擇訂單狀態" size="large" style="width: 240px" class="mr10 mb10">
          <el-option v-for="item in stateOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
        <el-select v-model="cancel_state" placeholder="選擇用戶取消狀態" size="large" style="width: 240px" class="mr10 mb10">
          <el-option v-for="item in cancelOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
        <el-select v-model="service_id" placeholder="選擇服務項目" size="large" style="width: 240px" class="mr10 mb10">
          <el-option v-for="item in serveOptions" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
        <div class="align-center mb10">
          <el-date-picker v-model="start" type="datetime" placeholder="开始时间" value-format="YYYY-MM-DD HH:mm:ss" />
          <span class="plr10 clr9fa">--</span>
          <el-date-picker v-model="end" type="datetime" placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss" />
        </div>

      </div>
      <div class="noshrink">
        <el-button class="btn2" @click="getOrderList()">查詢</el-button>
        <el-button class="btn1" @click="reset()">重置</el-button>
      </div>
    </div>
    <div class="bgblueg ptb20 radius10 w100p mt15 pl10 bold clr3c4">
      訂單總金額：{{ total_money }}元

    </div>
    <div class="mt15 radius10 hidden pb15 flex1 bgwhite  flex-col w100p">
      <div class="flex1 w100p ">
        <el-table :data="tableData" style="width: 100%" cell-class-name="tbcell" header-cell-class-name="hdcell">
          <el-table-column prop="uid" label="用戶訊息" min-width="200">
            <template #default="scope">
              <span @click="showUser(scope.row.user.id)">{{ scope.row.user.phone }}(ID:{{ scope.row.user.id }})</span>

            </template>
          </el-table-column>
          <el-table-column prop="touser" label="陪玩訊息" min-width="200">
            <template #default="scope">
              <span @click="showUser(scope.row.touser.id)">{{ scope.row.touser.phone }}(ID:{{ scope.row.touser.id }})</span>

            </template>
          </el-table-column>
          <el-table-column prop="address" label="下單地址" width="200" />
          <el-table-column prop="distance" label="距離(Km)" width="120" />
          <el-table-column prop="service_name" label="服務項目" width="150" />
          <el-table-column prop="fare_price" label="車費價格" sortable width="150" />
          <el-table-column prop="server_price" label="服務價格" sortable width="150" />
          <el-table-column prop="total_price" label="縂價格" sortable width="150" />
          <el-table-column prop="order_no" label="訂單號" width="200" />
          <el-table-column prop="" label="狀態" width="120">
            <template #default="scope">
              <template v-if="scope.row.is_bidding != 1">
                <div @click="dialogShow(2, scope.row)" v-if="scope.row.state == 4"><span class="red">{{
                  scope.row.state_text }}</span><el-icon>
                    <View />
                  </el-icon></div>
                <div v-if="scope.row.state == 2"><span class="yellow">{{ scope.row.state_text }}</span></div>
                <div v-if="scope.row.state == 3" @click="dialogShow(3, scope.row)"><span class="blue">{{ scope.row.state_text }}</span>
                  <el-icon>
                    <View />
                  </el-icon>
                </div>
                <div v-if="scope.row.state == 1"><span class="green">{{ scope.row.state_text }}</span></div>
              </template>


              <div @click="dialogShow(3, scope.row)" v-if="scope.row.is_bidding == 1"><span class="purple">{{
                scope.row.is_bidding_text }}</span>
                <el-icon>
                  <View />
                </el-icon>
              </div>

            </template>
          </el-table-column>
          <el-table-column prop="created_at" label="創建時間" width="200" />
          <el-table-column prop="address" label="接單時間" width="200" />
          <el-table-column prop="length" label="下單小時" width="120" />
          <el-table-column prop="address" label="完成時間" width="200" />

          <el-table-column prop="address" label="用戶取消狀態" width="200">
            <template #default="scope">
              <div v-if="scope.row.cancel_state != 0"><span class="blue">{{ scope.row.cancel_state_text }}</span></div>
              <div v-if="scope.row.cancel_state == 0"><span class="darkgreen">{{ scope.row.cancel_state_text }}</span>
              </div>

            </template>
          </el-table-column>
          <el-table-column prop="address" label="聊天記錄" width="120">
            <template #default="scope">
              <div class="clr3c4" @click="dialogShow(1, scope.row)">
                <el-icon size="25">
                  <Tickets />
                </el-icon>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="address" label="是否競標單" width="120">
            <template #default="scope">
              <span>{{ scope.row.is_bidding == 0 ? '否' : '是' }}</span>

            </template>
          </el-table-column>

          <el-table-column prop="address" label="操作" width="120" fixed="right">
            <template #default="scope">
              <div class="align-center">
                <div class=" pointer" @click="dialogShow(4, scope.row)">分成詳情</div>
                <!-- <span class="plr10">|</span>
                <div class="red">取消訂單</div> -->
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="plr20 mlauto mt20">
        <el-pagination v-model:current-page="currentPage1" prev-text="上一頁" next-text="下一頁" :default-page-size="15" :small="small" :disabled="disabled" :background="true" layout="prev, pager, next, jumper" :total="total1" @current-change="handleCurrentChange1" />
      </div>

    </div>
    <el-dialog v-model="dialogTableVisible" :title="dialogType == 1 ? '查看聊天記錄' : dialogType == 2 ? '取消原因' : dialogType == 3 ? '競標詳情' : '分成詳情'">

      <template v-if="dialogType == 1">


        <div class="mt20">
          <el-table :data="chatTableData" cell-class-name="dialogtbcell" header-cell-class-name="dialoghdcell">
            <el-table-column property="uid" label="發送者" width="100" />
            <el-table-column property="touid" label="接收者" width="100" />
            <el-table-column property="content" label="内容" min-width="300">

            </el-table-column>
            <el-table-column property="created_at" label="創建時間" width="200" />

          </el-table>
          <div class="  plr20 justify-between mt20">
            <div style="flex:1;" class="clr3c4 bold">總共{{ total2 }}條</div>
            <el-pagination v-model:current-page="currentPage2" prev-text="上一頁" next-text="下一頁" :default-page-size="15" :small="small" :disabled="disabled" :background="true" layout="prev, pager, next, jumper" :total="total2" @current-change="handleCurrentChange2" />
          </div>
        </div>

      </template>
      <template v-if="dialogType == 2">
        <div class="flex-col mt40 pb20">
          <div class="align-center">
            <span class="w100x tr mt10 clr3c4" style="align-self: start;">取消原因:</span>
            <div class="input clr3c4" style="min-height:150px;width: 400px;">{{ data.cancelReason }}</div>
          </div>
        </div>
      </template>
      <!-- 競標詳情 -->
      <template v-if="dialogType == 3">
        <div class="mt20">
          <el-table :data="bidTableData" cell-class-name="dialogtbcell" header-cell-class-name="dialoghdcell">
            <el-table-column property="uid" label="出價人訊息" min-width="200" />
            <el-table-column property="created_at" label="出價時間" min-width="200" />
            <el-table-column property="total_price" label="出價金額" width="150" />

          </el-table>
          <div class="  plr20 justify-between mt20">
            <div style="flex:1;" class="clr3c4 bold">總共{{ total3 }}條</div>
            <el-pagination v-model:current-page="currentPage3" prev-text="上一頁" next-text="下一頁" :default-page-size="15" :small="small" :disabled="disabled" :background="true" layout="prev, pager, next, jumper" :total="total3" @current-change="handleCurrentChange3" />
          </div>
        </div>
      </template>
      <!-- 分成詳情 -->
      <template v-if="dialogType == 4">
        <div class="flex-col mt40 pb20">
          <div class="align-center">
            <span class="w100x tr">平臺分成:</span>
            <div class="input">{{ data.plat_income }}</div>
          </div>
          <div class="align-center mt20">
            <span class="w100x tr">經紀人分成:</span>
            <div class="input">{{ data.agent_income }}</div>
          </div>
          <div class="align-center mt20">
            <span class="w100x tr">陪玩分成:</span>
            <div class="input">{{ data.playwith_income }}</div>
          </div>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { Search } from '@element-plus/icons-vue'
import { ref, onMounted, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router';
import { orderList, chatList, biddingList } from '@/api/order.js'
import { classifyList } from '@/api/player.js'
import { inject } from 'vue';
const showUser = inject('showUser');
const route = useRoute()
onMounted(() => {
  if (route.query.uid) {
    touid.value = route.query.uid
    getOrderList()
  } else {
    getOrderList()
  }

  getClassifyList()
})
const data = reactive({
  uid: '',
  touid: '',
  currentId: '',
  cancelReason: '',
  orderid: '',
  playwith_income: '',
  agent_income: '',
  plat_income: ''
})
// 订单记录
const getOrderList = () => {
  orderList({
    uid: uid.value,
    touid: touid.value,
    order_no: order_no.value,
    state: state.value,
    cancel_state: cancel_state.value,
    service_id: service_id.value,
    start: start.value,
    end: end.value
  }).then((res) => {
    console.log('订单记录', res)
    tableData.value = res.data.data
    total1.value = res.data.total
    total_money.value = res.total
  })
}
// 聊天记录
const getChatList = () => {
  chatList({
    uid: data.uid,
    touid: data.touid
  }).then((res) => {
    console.log('聊天记录', res)
    chatTableData.value = res.data.data
    total2.value = res.data.total
  })
}
// 竞价详情
const getBiddingList = () => {
  biddingList({
    orderid: data.orderid
  }).then((res) => {
    console.log('竞价详情', res)
    bidTableData.value = res.data.data
    total3.value = res.data.total
  })
}
// 分类列表
const getClassifyList = () => {
  classifyList({
    page: 1,
    per_page: 1000
  }).then((res) => {
    console.log('分类列表', res)
    serveOptions.value = res.data.data
  })
}
const stateOptions = [
  {
    label: '待核銷',
    value: '1'
  },
  {
    label: '競價中',
    value: '2'
  },
  {
    label: '已完成',
    value: '3'
  },
  {
    label: '已取消',
    value: '4'
  }
]
const cancelOptions = [
  {
    label: '未申請',
    value: '0'
  },
  {
    label: '已申請',
    value: '1'
  },
  {
    label: '取消申請',
    value: '2'
  },
]
const serveOptions = ref([])
const total_money = ref('')

const uid = ref('')
const touid = ref('')
const order_no = ref('')
const state = ref('')
const cancel_state = ref('')
const service_id = ref('')
const start = ref('')
const end = ref('')
const reset = () => {
  uid.value = ''
  touid.value = ''
  order_no.value = ''
  state.value = ''
  cancel_state.value = ''
  service_id.value = ''
  start.value = ''
  end.value = ''
  getOrderList()
}

const dialogTableVisible = ref(false)
const dialogType = ref(1)
const dialogShow = (type, row) => {
  console.log('dialog----show----')
  dialogType.value = type
  dialogTableVisible.value = true
  // 聊天记录
  if (type == 1) {
    data.uid = row.uid
    data.touid = row.touid
    getChatList()
  }
  // 取消原因
  if (type == 2) {
    data.currentId = row.uid
  }
  // 竞标详情
  if (type == 3) {
    data.orderid = row.id
    getBiddingList()
  }
  if (type == 4) {
    data.agent_income = row.agent_income
    data.plat_income = row.plat_income
    data.playwith_income = row.playwith_income
  }
}

const tableData = ref([])//订单记录
const chatTableData = ref([])//聊天记录
const bidTableData = ref([])//聊天记录

const tableRowClassName = () => {
  return 'tbrow'
}
const currentPage1 = ref(1)
const total1 = ref(0)
const currentPage2 = ref(1)
const total2 = ref(0)
const currentPage3 = ref(1)
const total3 = ref(0)
const handleCurrentChange1 = (val) => {
  console.log(`current page: ${val}`)
  getOrderList()
}
const handleCurrentChange2 = (val) => {
  console.log(`current page: ${val}`)
}
const handleCurrentChange3 = (val) => {
  console.log(`current page: ${val}`)
}
const router = useRouter()
const edit = (type) => {
  router.push({ path: 'signEdit' })
}
</script>

<style lang="scss" scoped>
.search {
  overflow: hidden;
  border-radius: 10px !important;
  // padding: 5px 0px;
}
</style>
<style></style>
 